{% extends "base.html" %}

{% block title %}分类管理 - 墨韵图书共享平台{% endblock %}

{% block content %}
<div class="admin-panel">
    <div class="row mb-4">
        <div class="col-md-12">
            <h2 class="ink-decoration mb-4">分类管理</h2>
            <p class="text-muted">在这里您可以管理图书分类，添加、编辑或删除分类。</p>
        </div>
    </div>
    
    <div class="row">
        <div class="col-md-3">
            <div class="card ink-card mb-4">
                <div class="card-header">
                    <h5 class="mb-0">管理菜单</h5>
                </div>
                <div class="list-group list-group-flush">
                    <a href="{{ url_for('admin_panel') }}" class="list-group-item list-group-item-action">
                        <i class="fas fa-tachometer-alt me-2"></i> 仪表盘
                    </a>
                    <a href="{{ url_for('admin_books') }}" class="list-group-item list-group-item-action">
                        <i class="fas fa-book me-2"></i> 图书管理
                    </a>
                    <a href="{{ url_for('admin_users') }}" class="list-group-item list-group-item-action">
                        <i class="fas fa-users me-2"></i> 用户管理
                    </a>
                    <a href="{{ url_for('admin_categories') }}" class="list-group-item list-group-item-action active">
                        <i class="fas fa-tags me-2"></i> 分类管理
                    </a>
                    <a href="{{ url_for('admin_statistics') }}" class="list-group-item list-group-item-action">
                        <i class="fas fa-chart-bar me-2"></i> 使用统计
                    </a>
                </div>
            </div>
        </div>
        
        <div class="col-md-9">
            <div class="card ink-card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0">分类列表</h5>
                    <a href="{{ url_for('create_category') }}" class="btn btn-sm btn-primary">
                        <i class="fas fa-plus me-1"></i> 添加分类
                    </a>
                </div>
                <div class="card-body">
                    {% if categories %}
                        <div class="table-responsive">
                            <table class="table">
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>名称</th>
                                        <th>描述</th>
                                        <th>图书数量</th>
                                        <th>创建时间</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for category in categories %}
                                        <tr>
                                            <td>{{ category.id }}</td>
                                            <td>{{ category.name }}</td>
                                            <td>{{ category.description|truncate(50) }}</td>
                                            <td>{{ category.books|length }}</td>
                                            <td>{{ category.created_at.strftime('%Y-%m-%d') }}</td>
                                            <td>
                                                <div class="btn-group btn-group-sm">
                                                    <button type="button" class="btn btn-outline-primary" data-bs-toggle="modal" data-bs-target="#editModal{{ category.id }}">
                                                        <i class="fas fa-edit"></i>
                                                    </button>
                                                    <button type="button" class="btn btn-outline-danger" data-bs-toggle="modal" data-bs-target="#deleteModal{{ category.id }}">
                                                        <i class="fas fa-trash"></i>
                                                    </button>
                                                </div>
                                                
                                                <!-- 编辑模态框 -->
                                                <div class="modal fade" id="editModal{{ category.id }}" tabindex="-1" aria-hidden="true">
                                                    <div class="modal-dialog">
                                                        <div class="modal-content">
                                                            <div class="modal-header">
                                                                <h5 class="modal-title">编辑分类</h5>
                                                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                                            </div>
                                                            <form action="{{ url_for('edit_category', category_id=category.id) }}" method="post">
                                                                <div class="modal-body">
                                                                    <div class="mb-3">
                                                                        <label for="name{{ category.id }}" class="form-label">分类名称</label>
                                                                        <input type="text" class="form-control" id="name{{ category.id }}" name="name" value="{{ category.name }}" required>
                                                                    </div>
                                                                    <div class="mb-3">
                                                                        <label for="description{{ category.id }}" class="form-label">描述</label>
                                                                        <textarea class="form-control" id="description{{ category.id }}" name="description" rows="3">{{ category.description }}</textarea>
                                                                    </div>
                                                                </div>
                                                                <div class="modal-footer">
                                                                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                                                                    <button type="submit" class="btn btn-primary">保存</button>
                                                                </div>
                                                            </form>
                                                        </div>
                                                    </div>
                                                </div>
                                                
                                                <!-- 删除模态框 -->
                                                <div class="modal fade" id="deleteModal{{ category.id }}" tabindex="-1" aria-hidden="true">
                                                    <div class="modal-dialog">
                                                        <div class="modal-content">
                                                            <div class="modal-header">
                                                                <h5 class="modal-title">删除分类</h5>
                                                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                                            </div>
                                                            <div class="modal-body">
                                                                <p>您确定要删除分类"{{ category.name }}"吗？</p>
                                                                {% if category.books|length > 0 %}
                                                                <div class="alert alert-warning">
                                                                    <i class="fas fa-exclamation-triangle me-2"></i>
                                                                    警告：该分类下有 {{ category.books|length }} 本图书，删除分类会导致这些图书变为未分类状态。
                                                                </div>
                                                                {% endif %}
                                                            </div>
                                                            <div class="modal-footer">
                                                                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                                                                <form action="{{ url_for('delete_category', category_id=category.id) }}" method="post">
                                                                    <button type="submit" class="btn btn-danger">删除</button>
                                                                </form>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </td>
                                        </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    {% else %}
                        <p class="text-center text-muted">暂无分类</p>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %} 